<script setup lang="ts">
  import { defineProps, ref, reactive, watch } from 'vue';
  import { TableRowSelection } from '@arco-design/web-vue';
  import { useBooleanStore } from '@/store';

  const props = defineProps<{
    tableData: any;
  }>();
  const columns: any[] = [
    {
      title: '序号',
      dataIndex: 'PAGEHELPER_ROW_ID',
      key: 'PAGEHELPER_ROW_ID',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
      align: 'center',
      fixed: 'left',
    },
    {
      title: '巷道',
      dataIndex: 'rowNo',
    },
    {
      title: '货位编码',
      dataIndex: 'shelvesCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '操作盘编码',
      dataIndex: 'palletCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '是否移库位置',
      dataIndex: 'transferPosition',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '货位状态',
      dataIndex: 'status',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '是否空箱',
      dataIndex: 'isEmpty',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },

    {
      title: '依赖货位',
      dataIndex: 'dependShelvesCode',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '最后修改时间',
      dataIndex: 'lastUpdateDate',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '货位高度',
      dataIndex: 'height',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '货位承受最大重量',
      dataIndex: 'weight',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '最后修改人',
      dataIndex: 'lastUpdatedBy',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '货品高度',
      dataIndex: 'heightOfGoods',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
    {
      title: '备注',
      dataIndex: 'remark',
      sortable: {
        sortDirections: ['ascend', 'descend'],
      },
    },
  ];
  const selectedKeys = ref<any>([]);
  const rowSelection: TableRowSelection = reactive({
    type: 'checkbox',
    showCheckedAll: true,
    onlyCurrent: true,
  });

  const booleanStore = useBooleanStore();
  // 创建一个响应式的本地副本
  const localTableData = ref<any[]>([]);
  // 数据库一共有多少条
  const total = ref<number>(0);
  const pageNum = ref<number>(1);
  const pageSize = ref<number>(10);

  // 监听props.tableData的变化，并更新本地副本
  watch(
    () => props.tableData,
    () => {
      localTableData.value = props.tableData.list;
      total.value = props.tableData?.total || 0;
    },
    { immediate: true }
  );

  function change(e: any) {
    pageNum.value = e;
  }
  function changeSize(e: any) {
    pageSize.value = e;
  }
  // 监听页面变化
  watch([pageNum, pageSize], () => {
    booleanStore.setPage(pageNum.value, pageSize.value, 39);
  });
  watch(
    selectedKeys,
    () => {
      booleanStore.setListThree(selectedKeys);
    },
    { deep: true }
  );
  watch(
    () => booleanStore.boolValue4,
    () => {
      if (booleanStore.num4 === 5) selectedKeys.value = [];
    }
  );
</script>

<template>
  <div>
    <a-table
      v-model:selectedKeys="selectedKeys"
      :stripe="true"
      row-key="PAGEHELPER_ROW_ID"
      class="top-table"
      :pagination="false"
      :columns="columns"
      :data="localTableData"
      :row-selection="rowSelection"
    >
    </a-table>
    <div class="page-position">
      <a-pagination
        :total="total"
        :current="pageNum"
        :page-size="pageSize"
        show-total
        show-jumper
        show-page-size
        @change="change"
        @update:page-size="changeSize"
      />
    </div>
  </div>
</template>

<style scoped>
  .top-table {
    white-space: nowrap;
  }
  .page-position {
    margin-top: 20px;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>
